<?php
class mochigames {

/******************************************************************************/
function getGame($nameid) {
    $nameid = mysql_escape_string($nameid);

    $sql = "select * from " . DBTABLE . " where nameid = '$nameid'";
    $result = mysql_query($sql) or die(mysql_error());
    $rec = mysql_fetch_assoc($result);

    return $rec;
}
/******************************************************************************/
function trackGame($nameid) {
    $nameid = mysql_escape_string($nameid);

    $sql = "update " . DBTABLE . " set portal_plays = portal_plays + 1 where nameid = '$nameid'";
    $result = mysql_query($sql) or die(mysql_error());
}
/******************************************************************************/
function getCategories() {
    $sql = "select distinct cat from " . DBTABLE;
   
    $result = mysql_query($sql) or die(mysql_error());
    
    $cats = array();
    while ($rec = mysql_fetch_assoc($result)) {
        $cat = explode(', ', $rec['cat']);
        $cats = array_merge($cats, $cat);
        $cats = array_unique($cats);
    }

    sort($cats);
    return $cats;

}
/******************************************************************************/
function getGamesByCat($cat, $limit=100, $offset=0) {
    $sql = "select * from " . DBTABLE . " where cat like '%$cat%' order by time desc limit $limit offset $offset";

    $result = mysql_query($sql) or die (mysql_error());
    $games = array();

    while ($rec = mysql_fetch_assoc($result)) {
        $games[] = $rec;    
    }

    return $games;
}
/******************************************************************************/
function getNumGamesByCat($cat) {
    $sql = "select count(*) as numGames from " . DBTABLE . " where cat like '%$cat%'";

    $result = mysql_query($sql) or die (mysql_error());
    $rec = mysql_fetch_array($result);
    return $rec['numGames'];
}
/******************************************************************************/
}
